<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Fodderimg;
use app\admin\model\Fodderimglist;
use app\admin\model\Replyfanscategory;
use app\admin\model\Replyfanspool;
use think\facade\Session;
use think\Request;
use think\Db;
use app\admin\model\Fodderlink;
use think\facade\Env;

class Fodder extends Base
{
    public function _initialize()
    {
            parent::_initialize();
    }
    public function fodderlink()
    {
        return $this->fetch('fodder/fodderlink');
    }
    //获取素材链接
    public function getFodderLinkList()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Fodderlink::where('delstatus',1)
            ->where('ghid|name','like','%'.$key.'%')
            ->page($page,$size)->order('createtime desc')
            ->select();
        $pagecount = Fodderlink::where('delstatus',1)->where('ghid|name','like','%'.$key.'%')->count();
        return $this->api_resp_success('',array('count'=>$pagecount),$list);
    }
    //删除素材链接
    public function delFodderLink()
    {
        $ids = $this->request->post('ids');
        if($ids == ''){
            return $this->api_resp_error('请选择要删除的链接');
        }
        $udata = [
            'delstatus'  => 4,
            'updatetime' => date('Y-m-d H:i:s',time())
        ];
        $res = Fodderlink::where('id','in',$ids)->update($udata);
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败','',$res);
        }
    }
    //图片素材
    public function fodderimg()
    {
        return $this->fetch('fodder/fodderimg');
    }
    //获取图片素材列表
    public function getFodderImgList()
    {
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Fodderimg::where('delstatus',1)
            ->page($page,$size)
            ->order('createtime desc')
            ->select();
        foreach ($list as $k=>$v){
            if($v['img_loca'] == 0){
                $list[$k]['img_loca_name'] = '封面图';
            }else if($v['img_loca'] == 1){
                $list[$k]['img_loca_name'] = '位置一';
            }else if($v['img_loca'] == 2){
                $list[$k]['img_loca_name'] = '位置二';
            }else if($v['img_loca'] == 3){
                $list[$k]['img_loca_name'] = '位置三';
            }
            $sunname = Replyfanscategory::where('id',$v['category_sum'])->value('categoryname');
            $childname = Replyfanscategory::where('id',$v['category_child'])->value('categoryname');
            $poolname = Replyfanspool::where('id',$v['accountpool'])->value('poolname');
            if($childname == null){
                $childname = '';
            }
            if($poolname == null){
                $poolname = '';
            }
            $list[$k]['sumname']   = $sunname;
            $list[$k]['childname'] = $childname;
            $list[$k]['poolname']  = $poolname;
            $imglist = Fodderimglist::where(['fodderid'=>$v['id'],'delstatus'=>1])->select();
            $html = '';
            foreach ($imglist as $ke=>$val){
                $html = $html.'<img src="'.$val["imgurl"].'" class="img_list">';
            }
            $list[$k]['img_list']  = $html;
        }
        $pagecount = Fodderimg::where('delstatus',1)->count();
        return $this->api_resp_success('',array('count'=>$pagecount),$list);
    }
    //删除图片素材
    public function delFodderImg()
    {
        $ids = $this->request->post('ids');
        if($ids == ''){
            return $this->api_resp_error('请选择要删除的素材');
        }
        $res = Fodderimg::where('id','in',$ids)->update(['delstatus'=>4]);
        if($res){
            $result = Fodderimglist::where('fodderid','in',$ids)->update(['delstatus'=>4]);
            if($result){
                return $this->api_resp_success('删除成功','',$res);
            }else{
                //返回操作
                $rest = Fodderimg::where('id','in',$ids)->update(['delstatus'=>1]);
                return $this->api_resp_error('删除失败');
            }
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //添加图片素材
    public function addFodderImg()
    {
        $category = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        $this->assign('list',$category);
        return $this->fetch('fodder/addFodderImg');
    }
    public function doAddFodderImg()
    {
        $data = $this->request->param();
        foreach ($data['sumlist'] as $k=>$v){
            $child = $data['childlist'][$k];
            $pool = $data['poollist'][$k];
            //如果子分类和账号池没选择则为空
            if(isset($child)){
                $childs = $child;
            }else{
                $childs = '';
            }
            if(isset($pool)){
                $pools = $pool;
            }else{
                $pools = '';
            }
            $udata = [
                'img_loca'       => $data['place'],
                'category_sum'   => $v,
                'category_child' => $childs,
                'accountpool'    => $pools,
                'createtime'     => date('Y-m-d H:i:s',time())
            ];
            $fodderid = Fodderimg::insertGetId($udata);
            if($fodderid){
                foreach ($data['img'] as $ke=>$val){
                    $idata = [
                        'fodderid' => $fodderid,
                        'imgurl' => $val,
                        'createtime'     => date('Y-m-d H:i:s',time())
                    ];
                    $res = Fodderimglist::insert($idata);
                }
            }
        }
        if($res){
            return $this->api_resp_success('保存成功','',$res);
        }else{
            return $this->api_resp_error('保存失败');
        }
    }
    //图片素材上传图片
    public function uploadImg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = $this->request->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $rootpath = Env::get('root_path');
        $info = $file->move($rootpath .'public/static/uploads');
        if($info){
            // 成功上传后 获取上传信息
            $files = 'http://wechat.nanxuncn.cn/static/uploads/'.$info->getSaveName();
            $file = $rootpath.'public/static/uploads/'.$info->getSaveName();
            return json(array('code' => 0, 'parame'=>$file , 'msg' => $files));
        }else{
            // 上传失败获取错误信息
            return json(array('code'=>4,'msg'=>$file->getError()));
        }
    }
    public function delUploadImg()
    {
        $ulink = $this->request->post('ulink');
        $res = unlink($ulink);
        if($res){
            return $this->api_resp_success('','','');
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //获取子分类下的账号池
    public function getFodderPool()
    {
        $id = $this->request->post('cid');
        $list = Replyfanspool::where(['category_child'=>$id,'delstatus'=>1])->select();
        return $this->api_resp_success('','',$list);
    }
    //编辑图片素材
    public function editFodderImg()
    {
        $id = $this->request->param('id');
        $data = Fodderimg::where('id',$id)->find();
        $imglist = Fodderimglist::where(['fodderid'=>$data['id'],'delstatus'=>1])->select();
        $category = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        $categorychild = Replyfanscategory::where(['delstatus'=>1,'level'=>2,'pid'=>$data['category_sum']])->select();
        $pool = Replyfanspool::where(['delstatus'=>1,'category_child'=>$data['category_child']])->select();
        $this->assign('list',$category);
        $this->assign('listchild',$categorychild);
        $this->assign('pool',$pool);
        $this->assign('data',$data);
        $this->assign('imglist',$imglist);
        return $this->fetch('fodder/editFodderImg');
    }
    //编辑图片素材上传图片
    public function uploadeditImg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $uid = $this->request->param('id');
        $file = $this->request->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $rootpath = Env::get('root_path');
        $info = $file->move($rootpath .'public/static/uploads');
        if($info){
            // 成功上传后 获取上传信息
            $files = 'http://wechat.nanxuncn.cn/static/uploads/'.$info->getSaveName();
            $file = $rootpath.'public/static/uploads/'.$info->getSaveName();
            $udata = [
                'imgurl' => $files,
                'fodderid' => $uid,
                'createtime' => date('Y-m-d H:i:s',time())
            ];
            Fodderimglist::insert($udata);
            return json(array('code' => 0, 'parame'=>$file , 'msg' => $files));
        }else{
            // 上传失败获取错误信息
            return json(array('code'=>4,'msg'=>$file->getError()));
        }
    }
    //删除编辑时候的图片
    public function deleditUploadImg()
    {
        $ulink = $this->request->post('ulink');
        $uid = $this->request->post('uid');
        $root = Env::get('root_path');
        $rootpath = $root.'public';
        $path = str_replace('http://wechat.nanxuncn.cn',$rootpath,$ulink);
        $res = unlink($path);
        if($res){
            Fodderimglist::where('id',$uid)->delete();
            return $this->api_resp_success('','','');
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //保存编辑图片素材的信息
    public function doEditFodderImg()
    {
        $data = $this->request->param();
        $udata = [
            'img_loca'       => $data['place'],
            'category_sum'   => $data['sum'],
            'category_child' => $data['child'],
            'accountpool'    => $data['pool'],
            'updatetime'     => date('Y-m-d H:i:s',time()),
        ];
        $res = Fodderimg::where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('编辑成功','',$res);
        }else{
            return $this->api_resp_error('编辑失败');
        }
    }
}
